<template>
  <div style="height: 100vh; overflow: hidden">
    <div style="width: 500px; height: 400px; background-color: white; border-radius: 15px; margin: 100px auto; padding: 50px">
      <div style="margin: 20px; text-align: center; font-size: 30px; font-weight: bold; color: dodgerblue">登 录</div>
      <el-form :model="admin" ref="loginForm" :rules="rules" >
        <el-form-item prop="username" style="padding-top: 10px">
          <el-input placeholder="请输入账号" prefix-icon="el-icon-user" size="medium" v-model="admin.username"></el-input>
        </el-form-item>
        <el-form-item prop="password" style="padding-top: 20px">
          <el-input placeholder="请输入密码" show-password prefix-icon="el-icon-lock" size="medium" v-model="admin.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
              style="width: 100%; background-color: dodgerblue; border-color: dodgerblue; color: white; font-size: 18px; font-weight: bold;"
              size="medium"
              type="primary"
              @click="login('loginForm')"
          >
            点击登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import request from "@/utils/request";
import Cookies from 'js-cookie'


export default {
  name: "Login",
  data() {
    //检查是否输入用户名
    var checkUserName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('用户名不能为空'));
      }else {
        callback();
      }
    };
    //检查联系电话
    var checkPassword = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('密码不能为空'));
      }else {
        callback();
      }
    };
    return {
      admin: {},
      loginForm: {
        username:'',
        password:'',
      },
      rules: {
        username: [
          {validator: checkUserName, trigger: 'blur'},
        ],
        password: [
          {validator: checkPassword, trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    login(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          request.post('/admin/login', this.admin).then(res => {
            if (res.code === '200') {
              this.$notify.success("登录成功！")
              if (res.data !== null) {
                Cookies.set('admin',JSON.stringify(res.data))
              }
              this.$router.push('/')
            } else {
              this.$notify.error(res.msg)
            }
          })
        }
      })
    }
  }

}
</script>



<style scoped>

</style>